package com.dnui.seven2.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnui.seven2.common.R;
import com.dnui.seven2.entity.Merch;
import com.dnui.seven2.service.MerchService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/merch")
@Slf4j
public class MerchController {

    @Autowired
    private MerchService merchService;

    /**
     * 新增周边
     * @param merch
     * @return
     */

    @PostMapping("/save")
    public R<String> saveMerch(@RequestBody Merch merch){
        merchService.save(merch);
        return R.success("添加周边成功");
    }

    /**
     * 周边分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){

        //构造分页构造器
        Page<Merch> pageInfo=new Page(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Merch> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.isNotEmpty(name),Merch::getMerchName,name);

        //执行查询
        merchService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    @DeleteMapping("/delete/{merchId}")
    public R<String> delete(@PathVariable Long merchId){
        merchService.delete(merchId);
        return R.success("删除成功");
    }

    @PutMapping("/update")
    public R<String> update(@RequestBody Merch merch){
        merchService.update(merch);
        return  R.success("修改成功");
    }

    @GetMapping("/getById/{merchId}")
    public R<Merch> getById(@PathVariable Long merchId){
        log.info("根据id查询员工信息");
        Merch merch=merchService.getById(merchId);
        return R.success(merch);
    }

    @GetMapping("/selectAll")
    public R<List<Merch>> selectAll(){
        List<Merch> merches = merchService.selectAll();
        return  R.success(merches);
    }
}
